﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Platform.ObjectModel.Design
{
	/// <summary>
	/// Realiza a construção de um novo campo do documento
	/// </summary>
	public class FieldDescriptorBuilder
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="qualifiedName"></param>
		internal FieldDescriptorBuilder( DataObjectName qualifiedName, Schema schema )
		{
			Debug.Assert( qualifiedName != null );
			Debug.Assert( schema != null );

			this.Field = new FieldDescriptor( qualifiedName, qualifiedName.Name );
			this.Schema = schema;
		}

		public FieldDescriptor Field
		{ get; protected set; }

		public Schema Schema
		{ get; private set; }

		public FieldDescriptorBuilder IsKey()
		{
			this.Schema.FieldKey = this.Field.QualifiedName;
			return this;
		}

		public FieldDescriptorBuilder FriendlyName( string name )
		{
			if ( !String.IsNullOrEmpty( name ) )
				this.Field.FriendlyName = name;
			return this;
		}

		public FieldDescriptorBuilder WithRules( params RuleBase[] rules )
		{
			this.Field.Rules.AddRange( rules );
			return this;
		}

		public FieldDescriptorBuilder TypeOf( Type fieldType )
		{
			this.Field.Type = LibraryUtility.GetFieldType( fieldType );
			return this;
		}

		public FieldDescriptorBuilder TypeOf( FieldType type )
		{
			this.Field.Type = type;
			return this;
		}

		public FieldDescriptorBuilder Format( DataObjectName formaterName )
		{
			this.Field.FormatterName = formaterName;
			return this;
		}
	}


	public class FieldRefDescriptorBuilder : FieldDescriptorBuilder
	{

		internal FieldRefDescriptorBuilder( DataObjectName qualifiedName, Schema schema )
			: base( qualifiedName, schema )
		{
			this.Field = new FieldRefDescriptor( qualifiedName );
		}

		//public FieldRefDescriptorBuilder ChildSchema( Schema schema )
		//{
		//    ( ( FieldRefDescriptor )this.Field ).ChildSchema = schema.QualifiedName;
		//    return this;
		//}
	}
}
